﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Softyum.ForexAgent.Config;
using Softyum.ForexAgent.Forms;
using Softyum.ForexAgent.Properties;

namespace Softyum.ForexAgent
{
    public partial class OptionsForm : Form
    {
        private ConfigManager configManager;
        private Button btnTestPopupWindow;

        public OptionsForm()
        {
            configManager = Program.ConfigManager;
            InitializeComponent();
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
            //
            btnTestPopupWindow = new Button();
            btnTestPopupWindow.Text = "测试";
            btnTestPopupWindow.Click += btnTestPopupWindow_Click;
            //
            treeView1.ExpandAll();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // Remove all custom buttons
            if (-1 != flowLayoutPanel1.Controls.IndexOf(btnTestPopupWindow))
            {
                flowLayoutPanel1.Controls.Remove(btnTestPopupWindow);
            }

            //
            TreeNode selected = treeView1.SelectedNode;
            if (selected.Name.Equals("PopupWindow"))
            {
                propertyGrid1.SelectedObject = Program.ConfigManager.PopupWindowSettings;
                // Show Test
                flowLayoutPanel1.Controls.Add(btnTestPopupWindow);
            }
            else
            {
                this.propertyGrid1.SelectedObject = null;
            }
        }

        /// <summary>
        /// 测试弹出窗口按键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTestPopupWindow_Click(object sender, EventArgs e)
        {
            PopupMessageForm f = new PopupMessageForm();
            f.Show("标题", "内容");
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                Program.ConfigManager.Save();
            }
            finally
            {
                this.btnApply.Enabled = false;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // Check for change
            if (btnApply.Enabled)
            {
                btnApply.PerformClick();
            }
            // close
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Program.ConfigManager.Reload();
            this.Close();
        }

        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            this.btnApply.Enabled = true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            TreeNode selected = treeView1.SelectedNode;
            if (selected.Name.Equals("PopupWindow"))
            {
                DialogResult dr = MessageBox.Show("是否要重置弹出窗口设定?",
                    "确认", MessageBoxButtons.YesNo, 
                    MessageBoxIcon.Question);
                if (dr == System.Windows.Forms.DialogResult.Yes)
                {
                    PopupWindowSettings settings = 
                        Program.ConfigManager.PopupWindowSettings;
                    // reset
                    settings.Reset();
                    // rebinding
                    propertyGrid1.SelectedObject = settings;
                }
            }
        }

        private void resetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GridItem item = propertyGrid1.SelectedGridItem;
            if (item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject))
                propertyGrid1.ResetSelectedProperty();
        }

        private void resetContextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            GridItem item = propertyGrid1.SelectedGridItem;
            resetToolStripMenuItem.Enabled = 
                item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject);
        }
    }
}
